// packageA/pages/trainReportList/trainReportList.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import util from '../../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //状态选项
    optionState: [
      { text: '全部', value: -1 },
      { text: '待培训', value: 0 },
      { text: '已签字', value: 1 },
      { text: '已完成', value: 2 }
    ],
    //状态值
    valueState: -1,
    //列表
    list:[],
    //登录用户type
    userType:null,
    //需要添加的培训信息
    addInfo:{},
    //无数据
    showNothing:false,
    //登录用户
    user:null,
    //是否显示添加按钮
    showAddBtn:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = app.globalData.userInfo;
    this.setData({
      user: user,
      userType: user.departObj[0].type
    })

    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })
      
      //获取培训报告
      this.getList();
    }
  },

  //获取培训报告
  getList(){
    let param = {
      isAuth: false,
      className: 'trainingReport',
      data: {
        'order.orderNo': this.data.paramObj.orderNo,
        'order.equipmentNo': this.data.paramObj.equipmentNo,
        selectFields: ['id','order','state','sign','dataAuthority','retrain','trainingStaff','dutyPerson','trainType']
      }
    }

    //状态
    let valueState=this.data.valueState;
    if(valueState!=-1){
      param.data.state=valueState;
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('培训报告列表');
        console.log(result);        

        this.setData({
          list: result,
          addInfo: result[0],
          showAddBtn:true
        })
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //切换状态
  onChangeState({ detail }) {
    this.setData({
      valueState: detail 
    })
    //获取培训报告
    this.refreshListAndPage();
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],  
      showNothing: false
    })
    this.getList();
  },

  //跳转详情
  goToDetail(e){
    let detail=e.currentTarget.dataset.detail;
    let arr=[];
    if(detail.order && detail.order.categoryId){
      arr=detail.order.categoryId;
    }

    let obj={
      id: detail.id,
      categoryId: arr
    }    
    wx.navigateTo({
      url: "/packageA/pages/trainReportDetail/trainReportDetail?paramObj="+JSON.stringify(obj)
    })
  },

  //跳转费用
  goToCost(e){
    let detail=e.currentTarget.dataset.detail;
    let obj={
      id: detail.id
    }    
    wx.navigateTo({
      url: "/packageA/pages/trainReportCost/trainReportCost?paramObj="+JSON.stringify(obj)
    })
  },

  //添加
  onAdd(){
    //判断未培训
    let list=this.data.list;
    let flag=list.some((item)=>{
      return item.state==0;
    });
    
    if(flag){
      Toast.fail('还有待培训，不可添加新单');
    }else{
      Dialog.confirm({
        title: '确定添加培训报告吗'
      }).then(() => {
        let addInfo=this.data.addInfo; 
  
        let param={
          className:'trainingReport',
          data:{
            id: null,
            state: 0,
            trainingStaff: addInfo.dutyPerson,
            trainNo: "TR"+util.timeNum(),
            expirationTime: util.timeFormat(util.countNextDay(new Date(),3)), //当前时间加3天
            options: {
              content: "生成培训报告",
              timestamp: util.timeFormat(new Date())
            },
            appId: [app.globalData.myAppId],

            order: addInfo.order,
            retrain: addInfo.retrain,
            dataAuthority: addInfo.dataAuthority,
            trainType: addInfo.trainType,
            dutyPerson: addInfo.dutyPerson
          }
        }

        //console.log(param);

        CommonService.goSave(param).then(res=>{
          if (res.statusCode == 200 && res.data.success == true){
            Toast.success('添加成功');
            setTimeout(() => {
              this.getList();
            }, 1000);
          }else if(res.statusCode==202){
            Toast.fail('您操作太快了,请勿重复提交');
          }else{
            Toast.fail('操作失败');
          }
        }).catch((error) => {
          Toast.fail('操作失败');
        }) 

      }).catch(() => {    
      });
    }  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})